<?php

use Workerman\Worker;
use Workerman\Connection\TcpConnection;

require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/route/index.php';

$config = parse_ini_file(__DIR__ . "/config.ini", true);
$host = $config["login_server"]["host"];
$port = $config["login_server"]["port"];

$ws_worker = new Worker("websocket://{$host}:{$port}");

// 启动4个进程对外提供服务
$ws_worker->count = 4;

// 当收到客户端发来的数据后返回hello $data给客户端
$ws_worker->onMessage = function (TcpConnection $connection, $data) {
    // 接收的消息格式 (1、解压zip 2、AesCBC解密 3、data转json)
    $req_body = ["seq" => '', "name" => '', "msg" => [], "proxy" => ''];

    // 发送的消息格式
    $rsp_body = ["seq" => '', "name" => '', "msg" => [], "code" => ''];

    //转到对于的路由去处理业务
    $rsp_body = route_init($req_body, $rsp_body);

    // 向客户端发送hello $data
    $connection->send('hello ' . $data);
};

// 运行worker
Worker::runAll();